package com.wuseguang.springcloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.wuseguang.springcloud.Classcardservice.ClassCardFieldService;
import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.dao.jdbc.StudentFacesTwoJdbc;
import com.wuseguang.springcloud.dao.repository.StudentFacesTwoRepository;
import com.wuseguang.springcloud.entity.StudentFacesTwo;
import com.wuseguang.springcloud.service.StudentFacesTwoService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学生人脸2.0表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-08-03
 */
@Service
public class StudentFacesTwoServiceImpl implements StudentFacesTwoService {


    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private StudentFacesTwoRepository studentFacesTwoRepository;
    @Autowired
    private StudentFacesTwoJdbc studentFacesTwoJdbc;
    @Autowired
    private ClassCardService classCardServiceImpl;
    @Autowired
    private ClassCardFieldService classCardFieldServiceImpl;

    @Override
    public Result findStudentFaceTwo(String deviceid, Integer type) {

       // Map<String, Object> stringObjectMap = studentFacesTwoJdbc.queryDevice(deviceid);
        //查询符合条件的人脸数据
        String sql = "select  sfr.id,sft.student_id,sft.url,s.student_name from  student_faces_registration sfr " +
                "left join student_faces_two sft on(sfr.student_faces_id=sft.id)  left join student s on (s.id=sft.student_id) where sfr.deviceid=? "
                + (type == 1 ? "  and sfr.state=1" : "  and sfr.state=3");

        List<Map<String, Object>> list = studentFacesTwoJdbc.queryListMap(sql, deviceid);
        //返回封装
        Map<String, Object> map = new HashMap<>();
        //判断如果大于100
        if (list.size() > 100) {
            //状态还有
            map.put("state", 1);
            //获取到100
            map.put("list", list.subList(0, 100));
        } else {
            //状态无
            map.put("state", 0);
            //直接返回
            map.put("list", list);
            if(type==1){
                sql = " UPDATE bancard SET faceflag=0 WHERE deviceid=?";
            }else{
                sql = " UPDATE bancard SET delfaceflag=0 WHERE deviceid=?";
            }
            studentFacesTwoJdbc.updateByParameter(sql, deviceid);
        }


        return new Result(map);
    }

    @Override
    public Result informStudentFaceTwo(JSONArray faceList, String deviceid) {

        if (faceList == null || faceList.size() == 0) {
            return new Result(CodeUtil.TELL_THE_FACE_INFORMATION_IS_EMPTY);
        }
        Map<String, Object> stringObjectMap = studentFacesTwoJdbc.queryDevice(deviceid);

        //修改学生人俩信息sql
        String sql = "UPDATE `student_faces_registration` SET `state` = ?  WHERE `id` = ? ";
        //循环人脸信息
        for (Object string : faceList) {
            Map<String, Object> map = JSON.parseObject(string.toString());
            studentFacesTwoJdbc.updateByParameter(sql, "2".equals(map.get("state").toString()) ? map.get("state") : 1, map.get("id"));

        }
        return new Result();
    }
}
